discard	
 
set more off 
use "$healthsave\recoded dates_W7.dta", clear 


** construct var for prevalence in second wave

	gen secondwave = .
	gen nowaves = inwave1==1 
	forv i = 2/7 { 
		replace nowaves = nowaves + (inwave`i'==1) 
		replace secondwave = `i' if nowaves==2 & inwave`i' == 1 
	} 
	
	foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 
		gen ever`x'_2ndw = . 
		forv i = 2/7 { 
			replace ever`x'_2ndw = ever`x'_w`i' if secondw == `i' 
		}
	}
	
	/** for documentation **/

		cd "P:\ELSA\Healthdynamics\documentation\tables\" 
		
		* how many w0 reports not passed though to first ELSA report 
		
		preserve 
		
		tokenize 1 4 22 23 34 38
				
		foreach x in ca ps lu as ar bl {

			replace ever`x'_w0 = -1 if longillw0==-1 	/* missings */		
			replace	ever`x'_w0 =  0 if longillw0==1 | longillw0==2 
			replace ever`x'_w0 = -1 if illsm1<=-7 
		
		forv i = 1/6 {
			replace ever`x'_w0 = 1 if illsm`i'w0==`1'
		}
		
		mac shift 
		}		
		
		
		
		
		foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 
			gen HSE`x' = ever`x'_w0 == 1 if ever`x'_w0 == 1 | ever`x'_w0 == 0 
			gen HSEproblem`x' = . 
			gen firstw`x' = . 
			forv i = 1/7 { 
				replace firstw`x' = ever`x'_w`i' if firstwave == `i' & ever`x'_w`i' >=0 
				replace HSEproblem`x' = 1 if firstw`x' == 0 & ever`x'_w0==1 
			} 
		
		** only in 1 wave and no medication 
			gen hsecl_1wave_nomed_`x' = 1 if ever`x'_w0 == 1 & firstw`x' == 0 & nmed`x'_w0 ==0 & (inwave==11 | inwave==22 | inwave==33 | inwave==44 | inwave==55 | inwave==66 | inwave == 77) 
		** only in 1 wave and medication 
			gen hsecl_1wave_wmed_`x' = 1 if ever`x'_w0 == 1 & firstw`x' == 0 & nmed`x'_w0 ==1 & (inwave==11 | inwave==22 | inwave==33 | inwave==44 | inwave==55 | inwave==66 | inwave == 77) 
		** reports in 2nd wave (so first wave changed to 1) 
			gen hsecl_r2ndwave_`x' = 1 if ever`x'_w0 == 1 & firstw`x' == 0 & ever`x'_2ndw == 1 
		** doesn't report by 2nd wave (so HSE ignored) 
			gen hse_nor_2ndwave_cl`x' = 1 if ever`x'_w0 == 1 & firstw`x' == 0 & (ever`x'_2ndw == 0 | ever`x'_2ndw==-1) 
		}			
		
		keep idauniq HSE* HSEproblem* hsecl_1wave_nomed_* hsecl_1wave_wmed_* hsecl_r2ndwave_* hse_nor_2ndwave_cl* inwave* 
		ren inwave inwaveall 
		reshape long HSE HSEproblem hsecl_1wave_wmed_ hsecl_1wave_nomed_ hsecl_r2ndwave_ hse_nor_2ndwave_cl, i(idauniq) j(condition) s 
		
		tabout condition using HSE_cleaning_w7.txt , ///
			c(sum HSE sum HSEproblem sum hsecl_1wave_wmed_ sum hsecl_1wave_nomed_ sum hsecl_r2ndwave_ sum hse_nor_2ndwave_cl) sum replace 
		
		restore 
		
		
*******************************************************
** 1. UNDER-REPORTING IN FIRST WAVE OF QUESTIONS  *****
*******************************************************

	/**  In first wave many health conditions were underreported 
		 because of question format. Hence lots of problems with 
		 observations looking like this:  1 | 0 1 ? ? ? 
		 
		If observe 1 | 0 . . . .   change to 1 | 1 ....  **/

	lab define howcl 11 "Changed from HSE report" 	, modify 	
		
*	foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 
	foreach x in bp an mi hm hr di st {
		forv i = 1/7 {
			replace howcl`x'_w`i' = 11 	///
					if ever`x'_w`i' !=1 ///
					 & ever`x'_w0 == 1 & med`x'_w0 == 1 ///
					 & inwave==11*`i' 
			replace ever`x'_w`i' = 1 if ever`x'_w0 == 1 & med`x'_w0 == 1 & inwave==11*`i' 
		}
	}

	
	/**  If any reports of condition within 2 waves of entry 
		(eg by wave 2 if in both waves 1 and 2) change interim report to 1. **/    

		
	* foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 
	foreach x in bp an mi hm hr di st {
		
		forv i = 1/6 { 
						
			replace howcl`x'_w`i' = 11 /// 
					if ever`x'_w0 == 1 		///
					& ever`x'_w`i' ! = 1 	///
					& ever`x'_2ndw == 1 	///
					& alive_w`i' == 1 		///
					& firstwave == `i' 				
			
			replace ever`x'_w`i' = 1 		///
					if ever`x'_w0 == 1 		///
					& ever`x'_2ndw == 1  	///
					& alive_w`i' == 1 		///
					& firstwave == `i' 	
					
			replace ever`x'_w`i' = 1 ///
					if ever`x'_w0 == 1 ///
					& ever`x'_2ndw == 1  	///
					& alive_w`i' == 1 		///
					& secondwave > `i' 
					
		}
	}

	
	/** If no reports within two waves of entry, 
			assume HSE report was wrong and change it to zero. **/ 
	
	lab define howcl 12 "overridden because no ELSA reports" , modify 
	
	foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 
	
		replace howcl`x'_w0 = 12 if ever`x'_w0==1 & (ever`x'_2ndw == 0 | ever`x'_2ndw==-1) 
		replace ever`x'_w0=0 if ever`x'_w0==1 & (ever`x'_2ndw == 0 | ever`x'_2ndw==-1) 
		
	}


	foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 
	forv i = 1/7 { 
	forv j = 1/7 { 
		if `i' < `j' { 
		di in red "`x' `i' `j'" 
			assert ever`x'_w`i' <= ever`x'_w`j'	| ever`x'_w`i' ==. | ever`x'_w`j'==-1 
	}
	}
	}
	}
	
	/** we now override missing observations **/	
	
	lab define howcl 13 "missing overridden from other info" , modify 
	
	foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 
		forv i = 0/7 {   /* to be amended */
		forv j = 0/7 { 	 /* based on report in this wave */
			
			if `i' < `j' {
			
		** fill in zeros ** 		
		
				replace howcl`x'_w`i' = 13 ///
					if ever`x'_w`j' == 0 ///
						& alive_w`i' == 1 ///
						& (ever`x'_w`i' == . | ever`x'_w`i' == -1) 
				
				replace ever`x'_w`i' = 0 ///
					if    ever`x'_w`j' == 0 ///
						& alive_w`i' == 1 ///
						& (ever`x'_w`i' == . | ever`x'_w`i' == -1) 
	
			}
			
			
			if `i' > `j' { 
		
		** fill in ones ** 
			
		qui: 		replace howcl`x'_w`i' = 13 /// 
					if ever`x'_w`j' == 1 ///
						& alive_w`i' == 1 ///
						& (ever`x'_w`i' == . | ever`x'_w`i' == -1) 	
			
		qui: 		replace ever`x'_w`i' = 1 ///
					if ever`x'_w`j' == 1 ///
						& alive_w`i' == 1 ///
						& (ever`x'_w`i' == . | ever`x'_w`i' == -1) 	
				
			}
		}
		}
	}	

	foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 
		forv i = 0/7 { 
			replace ever`x'_w`i' = -1 if alive_w`i' ==1 & ever`x'_w`i' == . 
		}
	}	
	
 save "$healthsave\cleaned prevalence_w7.dta", replace
